package com.example.shop_2211.search

import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.History
import com.example.base.mvvm.BaseActivity
import com.example.shop_2211.R
import com.example.shop_2211.adapter.GoodsAdapter
import com.example.shop_2211.databinding.ActivitySearchBinding
import com.example.shop_2211.room.AppDBUtils
import com.google.android.material.chip.Chip

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    //搜索的参数
    @Autowired
    @JvmField
    var cid = 0
    @Autowired
    @JvmField
    var cname = "全部"//暂时用不到，分类时用到
    var keyword = ""
    var limit = 10
    var news = false
    var page = 1
    var priceOrder = ""
    var salesOrder = ""
    //
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.tvType.text = cname
       goodsAdapter = GoodsAdapter()
       binding.rv.adapter = goodsAdapter
       binding.rv.layoutManager = GridLayoutManager(this,2)
        viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
       //执行搜索功能
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){
                    keyword = query
                    viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
                    //点击回车后将关键词放入数据库中
                    AppDBUtils.getDb().getHisDao().insertHis(History(keyword,SPUtils.getInstance().getString("phone")))
                    showhis()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if(newText!=null){
                    keyword = newText
                    viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
                }
                return false
            }
        })
        //显示历史搜索记录
        showhis()
        //按价格排序
        binding.tvPrice.setOnClickListener {
            if(priceOrder==""){
                priceOrder = "asc"//按升序排序
                binding.tvPrice.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort3,0)
            }else if(priceOrder=="asc"){
                priceOrder = "desc"//按降序序排序
                binding.tvPrice.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort2,0)
            }else if(priceOrder=="desc"){
                priceOrder = ""
                binding.tvPrice.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort1,0)
            }
            //销量排序设为无序的
            salesOrder = ""
            binding.tvSales.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.drawable.sort1,0)
            viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //按销量排序
        binding.tvSales.setOnClickListener {
            if(salesOrder==""){
                salesOrder = "asc"//按升序排序
                binding.tvSales.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort3,0)
            }else if(salesOrder=="asc"){
                salesOrder = "desc"//按降序序排序
                binding.tvSales.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort2,0)
            }else if(salesOrder=="desc"){
                salesOrder = ""
                binding.tvSales.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,
                    R.drawable.sort1,0)
            }
            //价格排序设为无序的
            priceOrder = ""
            binding.tvPrice.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.drawable.sort1,0)
            viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
        //点击新品
        binding.tvIsNew.setOnClickListener {
            if(news){
                news = false
                binding.tvIsNew.setTextColor(Color.BLACK)
            }else{
                news = true
                binding.tvIsNew.setTextColor(Color.RED)
            }
            viewModel.search(cid, keyword, limit, news, page, priceOrder, salesOrder)
        }
    }

    private fun showhis() {
        //显示保存的历史记录
        val list = AppDBUtils.getDb().getHisDao().query()
        if(list.size>0){
            //将chipgroup中添加的数据先清空
            binding.cg.removeAllViews()
            list.forEach {
                //找到chip
                val view = LayoutInflater.from(this).inflate(R.layout.item_chip, null) as Chip
                var name = it.key
                view.findViewById<TextView>(R.id.name).text = it.key

                binding.cg.addView(view)//将view添加到chipgroup中
                //再次点击，能够查询
                view.setOnClickListener {
                    binding.sv.setQuery(name,true)
                }
                view.setOnCloseIconClickListener {
                    //删除数据库中的数据
                    AppDBUtils.getDb().getHisDao().deleteHis(History(name,SPUtils.getInstance().getString("phone")))
                    //移除页面中的数据
                    binding.cg.removeView(it)
                }
            }
        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel>  = SearchViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_search

}